var ip = "http://192.168.10.180:3001";
//function sub(upType,furl) {
function lodaing(){
	$('body').loading({
			loadingWidth:240,
			title:'请稍等!',
			name:'test',
			discription:'',
			direction:'column',
			type:'origin',
			// originBg:'#71EA71',
			originDivWidth:40,
			originDivHeight:40,
			originWidth:6,
			originHeight:6,
			smallLoading:false,
			loadingMaskBg:'rgba(0,0,0,0.2)'
		});
//		removeLoading('test');
}


function sub() {
	var ss = $("#hotelplace").val();
	if(ss=="n"||ss==null){
		alert("信息填写不完整");
			return false;
	}
	for(var i = 0; i < $("input").length; i++) {
		var c = $("input").eq(i).val();
		console.log(i)
		if(c == "") {
			alert("信息填写不完整");
			return false;
		}
	}
	for(var a = 0; a < $("input").length; a++) {
		var b = $("input").eq(a).attr("class");
		console.log(a)
		if(b == "red") {
			alert("填写有误！")
			return false;
		}
	}
	lodaing();
	var address_id = $("#hotelplace").val();
	var name = $("#name").val();
	var phone = $("#phone").val();
	var idcard = $("#idcard").val();
	var sex = $("#sex").val();
	var bir = $("#bir").val();
	var openid = $("#openid").val();
	var pic="";
	var formData = new FormData();
	formData.append("address_id", address_id);
	formData.append("name", name);
	formData.append("phone", phone);
	formData.append("idcard", idcard);
	formData.append("sex",sex);
	formData.append("bir", bir);
	formData.append("openid", openid);
	oFile = $('#files')[0].files[0];
	imgSize = oFile.size;

  	if(imgSize < 256 * 1024){
        formData.append("pic", oFile);
    } else {    // 图片压缩处理
        var reader = new FileReader(),
            maxWidth = 400,
            maxHeight= 400,
            suffix = oFile.name.substring(oFile.name.lastIndexOf('.') + 1);

        if(imgSize > 2 * 1024 * 1024){
            maxWidth = 800;
            maxHeight= 800;   
        }

        reader.onload = function(e) {
            var base64Img= e.target.result;
            //--执行resize。
            var _ir=ImageResizer({
                resizeMode:"auto",
                dataSource:base64Img,
                dataSourceType:"base64",
                maxWidth:maxWidth, //允许的最大宽度
                maxHeight:maxHeight, //允许的最大高度。
                onTmpImgGenerate:function(img){
                },
                success:function(resizeImgBase64,canvas){
                    var blob = dataURLtoBlob(resizeImgBase64);
                    formData.append('pic', blob, oFile['name']);
	$.ajax({
		type:"post",
		url: ip + "/api/v1/hotel_record",
		async:false,
		cache: false,
		processData: false,
		contentType: false,
		data:formData,
		complete:function(){
			removeLoading('test');
		},
		success: function(data) {
			console.log(data);
			alert(data.msg);
			window.location = "index2.html";
		}
	});
                }
            });
        };
        reader.readAsDataURL(oFile);
    }
}

function selectdata(surl, sdom, sdata) {
	$("#hotelplace").html("");
	console.log($("#hotelplace").html())
	lodaing();
	$.ajax({
		type: "get",
		url: ip + surl,
		async: true,
		data: sdata,
		complete:function(){
			removeLoading('test');
		},
		success: function(data) {
			var html = "<option value='n' selected=\"selected\">请选择</option>";
			var list = data.result;
			console.log(list.length);
			for(var i = 0; i < list.length; i++) {
				html += "<option value='" + list[i].id + "'>" + list[i].name + "</option>";
			}
			$("" + sdom + "").html(html);
		}
	});
}

//报警
function jb() {
	for(var i = 0; i < $("input").length; i++) {
		var c = $("input").eq(i).val();
		if(c == "") {
			alert("信息填写不完整");
			return false;
		}
	}
	for(var a = 0; a < $("input").length; a++) {
		var b = $("input").eq(a).attr("class");
		if(b == "red") {
			alert("填写有误！")
			return false;
		}
	}
lodaing();
	$.ajax({
		cache: true,
		type: "get",
		url: ip + "/api/v1/alarm_upload",
		data: $('form').serialize(), // 你的formid
		async: false,
		error: function(request) {
			alert("Connection error");
		},
		complete:function(){
			removeLoading('test');
		},
		success: function(data) {
			for(var i = 0; i < $("input").length; i++) {
				$("input").eq(i).val("");
			}
			alert(data.msg);
		}
	});
}

function query(){
	lodaing();
	var starTime = $("#starTime").val();
	var endTime = $("#endTime").val();
	var html="";
	$.ajax({
		type:"get",
		url: ip + "/api/v1/query_data",
		async:true,
		data:{
			begin_time:starTime,
			end_time:endTime
		},
		complete:function(){
			removeLoading('test');
		},
		success:function(data){
			console.log(data);
			var html="";
			var list = data.result;
			for (var i=0;i<list.length;i++) {
				html+='<li class="ui-li-has-thumb">\
				      	<a href="#" class="aft ui-btn ui-btn-icon-right ui-icon-carat-r"><img src="'+ip+list[i].pic_url+'" alt="img">\
				      	<p style="display:  inline;">'+list[i].name+'</p>&nbsp;&nbsp;&nbsp;&nbsp;\
				      	<p style="display: inline;">'+list[i].sex+'</p>\
				      	<p>Tell:'+list[i].phone+'</p>\
				      	<p>'+list[i].status+'</p>\
				      	</a>\
				      </li>';
			}
			$('#uldata').html(html);
		}
	});
}

var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" }

function isCardID(sId) {
	var iSum = 0;
	var info = "";
	if(!/^\d{17}(\d|x)$/i.test(sId)) return "你输入的身份证长度或格式错误";
	sId = sId.replace(/x$/i, "a");
	if(aCity[parseInt(sId.substr(0, 2))] == null) return "你的身份证地区非法";
	sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
	var d = new Date(sBirthday.replace(/-/g, "/"));
	if(sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return "身份证上的出生日期非法";
	for(var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
	if(iSum % 11 != 1) return "你输入的身份证号非法";
	return aCity[parseInt(sId.substr(0, 2))] + "," + sBirthday + "," + (sId.substr(16, 1) % 2 ? "男" : "女"); //此次还可以判断出输入的身份证号的人性别
	// return true;
}
